Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Datatypes

Python Bytearray

The bytearray data type in Python offers a mutable sequence of bytes. Think of it as the editable counterpart to the immutable bytes type. While both data types represent raw binary data, bytearrays allow you to change the individual bytes after creation.

Key Characteristics

Mutable: You can directly modify individual bytes within a bytearray, providing more flexibility than immutable bytes objects. ⯄ Representing Binary Data: Each element in a bytearray is an integer between 0 and 255, representing a single byte. ⯄ Encoding: Like bytes, bytearrays do not have an intrinsic character encoding. You'll need to use appropriate .decode() methods to interpret a bytearray as text.

Creating Bytearrays

Bytearray Literal: You can use the bytearray() constructor with a bytes literal
Creating bytearray using bytearray() constructor with a bytes literal bytearray = bytearray(b'Hello') print(bytearray)

Output

bytearray(b'Hello')
From a List of Integers
Creating bytearray with a list of integer data = bytearray([4, 18, 255]) print(data) print(type(data))

Output

bytearray(b'\x04\x12\xff')
From a String and Encoding:
Creating bytearray from a String and Encoding text = "Greetings!" encoded_bytearray = bytearray(text, 'utf-8') print(text) print(encoded_bytearray)

Output

Greetings! bytearray(b'Greetings!')

Operations on Bytearrays

Indexing and Slicing: Access and modify individual bytes using indexing and slicing, similar to lists or bytes objects Mutable Methods append(byte): Add a single byte extend(iterable): Add bytes from another iterable insert(index, byte): Insert a byte at a specific index Decoding: Use the decode() method to convert a bytearray back into a string (with a specified encoding)

Use Cases

⮞ Modifying Binary Data: When you need to transform or manipulate binary data structures in-place. ⮞ Building Binary Protocols: Creating structured binary data to send over networks or write to files. ⮞ I/O Buffers: Storing data temporarily before writing it to a file or network connection.

Key Difference from bytes

The primary distinction is mutability. If you don't need to modify the byte sequence after creation, it's often preferred to use the immutable bytes type.

Examples

Creating a bytearray from a string:
Creating a bytearray from a string example in python str = "Hello world!" array1 = bytearray(str, 'utf-8') array2 = bytearray(str, 'utf-16') print(array1) print(array2)

Output

bytearray(b'Hello world!') bytearray(b'\xff\xfeH\x00e\x00l\x00l\x00o\x00 \x00w\x00o\x00r\x00l\x00d\x00!\x00')
In this example, the bytearray() function encodes the string and converts it to a byte array object in Python using str.encode(). Creating a bytearray from an integer:
Creating a bytearray from an integer example in python size = 3 array1 = bytearray(size) print(array1)

Output

bytearray(b'\x00\x00\x00')
Here, an integer is passed to the bytearray() function, which creates an array of that size and initializes it with null bytes. Creating a bytearray from a byte object:
Creating a bytearray from a byte object example in python array1 = bytearray(b"abcd") for value in array1: print(value) array2 = bytearray(b"tutorialsbox.com") print("Count of o is:", array2.count(b"o"))

Output

97 98 99 100 Count of o is: 3
In this example, a byte object is passed to the bytearray() function. The read-only buffer of the byte object is used to initialize the byte array. Creating a bytearray from a list of integers:
Creating a bytearray from a list of integers example in python list = [1, 2, 3, 4] array = bytearray(list) print(array) print("Count of bytes:", len(array))

Output

bytearray(b'\x01\x02\x03\x04') Count of bytes: 4
Here, a Python list of integers is passed as a parameter to the bytearray() function. Creating a bytearray with no parameters:
Creating a bytearray with no parameters array = bytearray() print(array)

Output

bytearray(b'')
If no source is provided to the bytearray() function, an array of size 0 is created.

  📌TAGS

★python ★ datatypes ★ binary datatype ★ bytearray

Tutorials